package cn.lanqiao.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cn.lanqiao.entity.Admin;
import cn.lanqiao.entity.ApiResult;
import cn.lanqiao.entity.Login;
import cn.lanqiao.entity.Student;
import cn.lanqiao.entity.Teacher;
import cn.lanqiao.service.impl.LoginServiceImpl;
import cn.lanqiao.util.ApiResultHandler;
import cn.lanqiao.util.CacheMap;

@RestController
	//跨域
	@CrossOrigin(origins="*",maxAge=3600)
	public class LoginController {

	    @Autowired
	    private LoginServiceImpl loginService;

	    @PostMapping("/login")
	    public ApiResult login(@RequestBody Login login) {

	        String username = login.getUsername();
	        String password = login.getPassword();
	        //管理员
	        Admin adminRes = loginService.adminLogin(username, password);
	        if (adminRes != null) {
	            return ApiResultHandler.buildApiResult(200, "请求成功", adminRes);
	        }
           //老师
	        Teacher teacherRes = loginService.teacherLogin(username,password);
	        if (teacherRes != null) {
	            return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
	        }
          //学生
	        Student studentRes = loginService.studentLogin(username,password);
	        if (studentRes != null) {
	            return ApiResultHandler.buildApiResult(200, "请求成功", studentRes);
	        }

	        return ApiResultHandler.buildApiResult(400, "请求失败", null);
	    }

	    @PostMapping("student/register")
		public ApiResult register(@RequestBody Map<String, String> param, HttpServletRequest request){
			HttpSession session = request.getSession();
			Integer responseCode = 400;
			String message = "注册失败!";
			String code = param.get("code");
			String password = param.get("password");
			String username = param.get("username");
			String emailCode = CacheMap.CACHE_CODE_MAP.get(username);
			System.out.println(emailCode);
			Login login = new Login();
			login.setPassword(password);
			login.setUsername(username);
			System.out.println(request.getSession().getId());
			if(code != null && code.equals(emailCode)){
				message = loginService.register(login, code, request);
				if(message.equals("学生注册成功!")){
					responseCode = 200;
				}
			}else{
				return ApiResultHandler.buildApiResult(responseCode, "验证码或邮箱错误!", null);
			}
			return ApiResultHandler.buildApiResult(responseCode, message, null);
		}
	    @PostMapping("teacher/register")
	    public ApiResult teacherRegister(@RequestBody Map<String, String> param, HttpServletRequest request){
			HttpSession session = request.getSession();
			Integer responseCode = 400;
			String message = "注册失败!";
			String code = param.get("code");
			String password = param.get("password");
			String username = param.get("username");
			String emailCode = CacheMap.CACHE_CODE_MAP.get(username);
			Login login = new Login();
			login.setPassword(password);
			login.setUsername(username);
			System.out.println(emailCode);
			System.out.println(request.getSession().getId());
			if(code != null && code.equals(emailCode)){
				message = loginService.teacherRegister(login, code, request);
				if(message.equals("教师注册成功!")){
					responseCode = 200;
				}
			}else{
				return ApiResultHandler.buildApiResult(responseCode, "验证码或邮箱错误!", null);
			}
			return ApiResultHandler.buildApiResult(responseCode, message, null);
		}
	}